#include "temperature.h"
#include "map.h"

void vTaskCTRL_TEMPERATURE()
{
    portTickType xLastWakeTime;
    double temp_tmp[NB_SAMPLE_TEMP];
    char mes[32]; 
    double average_temp = 0;
    double mesured_temp = 0;
    uint8_t addr = 0;
    uint8_t temp_sample = 0;
    uint8_t temp_nb = 0;
    bool sample_state = TRUE;

    xLastWakeTime = xTaskGetTickCount();

    init_TEMPERATURE();
    wait(500);  // pour attendre que les broches s'initialise bien
    while(1)
    {
        for ( temp_nb = 0; temp_nb < _NB_SENSOR_TEMP; temp_nb++ )
        {
            switch(temp_nb)
            {
                case 1:
                    addr = TMP100_ADDR_0;
                    break;
                case 2:
                    addr = TMP100_ADDR_1;
                    break;
                case 3:
                    addr = TMP100_ADDR_2;
                    break;
                case 4:
                    addr = TMP100_ADDR_3;
                    break;
                default:
                    addr = TMP100_ADDR_0;
                    break;
            }
        
            /* Boucle de moyenage */
            for ( temp_sample = 0; temp_sample < NB_SAMPLE_TEMP; temp_sample++ )
            {
                if ( read_temp(&mesured_temp, addr) == TEMP_OK )
                {
                    temp_tmp[temp_sample] = mesured_temp;
                }
                else
                {
                    sample_state = FALSE;   /* Pour ne pas faire de moyenne en cas d'erreur */
                }
            
                wait(5000);
            }
        
            /* si pas d'erreur, calcul de la moyenne */
            if ( sample_state == FALSE )
            {
                send_message(TEMPERATURE_MODULE_ID, warning, "Temp 1 : ERROR");
            }
            else
            {
                for ( temp_sample = 0; temp_sample < NB_SAMPLE_TEMP; temp_sample++ )
                {
                    average_temp += temp_tmp[temp_sample];
                }
                average_temp /= NB_SAMPLE_TEMP;
                sprintf(mes, "Average Temp = %02.1f", average_temp);
                send_message(TEMPERATURE_MODULE_ID, normal, mes);
            }
        
            wait(60000);
        }
    }
}

void init_TEMPERATURE()
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
  NVIC_InitStructure.NVIC_IRQChannel = I2C2_ER_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  TMP100_Init();
}

TEMP_ERROR read_temp(double *temp, uint8_t addr)
{
    int32_t TempValue = 0;

    TempValue = TMP100_ReadTemp(addr);
    if ( (TempValue <= _TEMP_MAX) && (TempValue <= _TEMP_MAX) )
    {
        *temp = TempValue*_TEMP_RESOLUTION;
        return TEMP_OK;
    }
    else
    {
        return TEMP_NOK;
    }
}